<?php
class QuickToolService {
    private static $instance;

    public static function get() {
        if (!self::$instance) {
            self::$instance = new QuickToolService();
        }
        return self::$instance;
    }

    protected function __construct() {

    }

    /**
     * 给玩家补发道具
     * @param $userId
     * @param $itemArr
     * @param $numArr
     * @param $reqTime
     * @return array
     * @throws SzException
     */
    public function sendUserItem($userId,$itemArr,$numArr,$reqTime) {
        $res = array(
            'code' => 0,
            'items' => array(),
            'nums' => array(),
            'errMsg' => 'sendSuc!!',
        );

        //校验数组正确性
        if (empty($itemArr)){
            $res['errMsg'] = 'item is empty!';
            return $res;
        }
        if (empty($numArr)){
            $res['errMsg'] = 'num is empty!';
            return $res;
        }

        $num1 = count($itemArr);
        $num2 = count($numArr);
        if ($num1 != $num2){
            $res['errMsg'] = 'Two Arr Num Is Not Same!';
            return $res;
        }

        $tempItems = array();
        $itemSets = SzConfig::get()->loadAppConfig(ModuleItemConst::CONFIG_ITEM_DEFS, null, true);
        foreach ($itemSets as $k=>$itemSet){
            $tempItems[] = $itemSet['itemDefId'];
        }

        if (empty($tempItems)){
            $res['errMsg'] = 'module_item_def is empty!';
            return $res;
        }

        foreach ($itemArr as $key=>$itemId){
            if ($itemId <= 0){
                $res['errMsg'] = ($key+1).' itemId <= 0!';
                return $res;
            }
            if (!in_array($itemId,$tempItems)){
                $res['errMsg'] = $itemId.' is Err!';
                return $res;
            }
        }

        foreach ($numArr as $key=>$num){
            if ($num <= 0){
                $res['errMsg'] = ($key+1).' $num <= 0!';
                return $res;
            }
        }

        //给玩家补发道具
        $input = '';
        foreach ($itemArr as $key1=>$itemId1){
            if ($itemId1 == 11000001){//金币
                GameService::get()->handleEntity($userId, '7211'.$itemId1, $numArr[$key1],  true);
            }
            elseif ($itemId1 == 14000001){//经验
                GameService::get()->handleEntity($userId, '7214'.$itemId1, $numArr[$key1],  true);
            }
            elseif ($itemId1 == 10000001){//钻石
                GameService::get()->handleEntity($userId, '7210'.$itemId1, $numArr[$key1],  true);
            }
            elseif ($itemId1 == 19000001){//水
                GameService::get()->handleEntity($userId, '7251'.$itemId1, $numArr[$key1],  true);
            }
            else{
                GameService::get()->handleEntity($userId, '7201'.$itemId1, $numArr[$key1],  true);
            }

            $input .= $itemId1.':'.$numArr[$key1].'|';
        }
        CommonFunctionService::get()->reportResourceUseDetails(90000000,$userId,1,$input,'sendUserItem');//获得资源上报

        $res['items'] = $itemArr;
        $res['nums'] = $numArr;

        return $res;
    }
}
